<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    function __construct() {
        parent::__construct();
        $this->load->model('User_m', 'm');
        $this->load->model('Function_m', 'fun');
		 $this->pageSize = $this->config->item('pagenum');
    }

    public function onedit() {
        $id = $_COOKIE["userid"];
        if (!$id) {
            die("ID Err");
        }
        $detail = $this->m->getuserdetail($id);
        $this->tpl->assign("detail", $detail);
        $this->tpl->display("user/user_onedetail.tpl", 0, 0, 1);
    }

    public function savepassword() {
        $uid = $_COOKIE["userid"];
        $pass = $this->input->post('password');
        $id = $this->m->onsaveUser($uid, $pass);
        echo $id;
        die;
    }

    public function add() {

        //查所有店铺
        $this->tpl->assign("d_shop", $this->fun->getdatabaseslist('d_shop', '', 'id,name', ' order by name asc'));
        //查所有仓库
        $this->tpl->assign("overseawarehouse", $this->fun->getdatabaseslist('t_overseawarehouse', '', 'id,name', ' order by id desc'));
        //用户组
        $this->tpl->assign("usergroup", $this->fun->getdatabaseslist('d_group', "", '*', ' order by id desc'));
        $this->tpl->display("user/add.tpl");
    }

    public function saveadd() {

        $id = $this->m->saveedit();
        header("location:/user/edit?uid={$id}");
    }

    public function edit() {
        $uid = $this->input->get('uid');
        $detail = $this->m->getuserdetail($uid);
        $detail["seawarehouse"] = unserialize($detail["seawarehouse"]);
        $detail["shopname"] = unserialize($detail["shopname"]);
        $this->tpl->assign("detail", $detail);
        //查所有店铺

		
		$depts = $this->fun->getdatabaseslist("d_shop"," and del_flag =0 ","id,name,platfromname");
        foreach ($depts as $key => $rs) {
            $deptsname[$rs["platfromname"]][] = $rs;
        }
        $this->tpl->assign("d_shop", $deptsname);
        //查所有仓库
        $this->tpl->assign("overseawarehouse", $this->fun->getdatabaseslist('t_overseawarehouse', '', 'id,name', ' order by id desc'));
        //用户组
        $this->tpl->assign("usergroup", $this->fun->getdatabaseslist('d_group', "", '*', ' order by id desc'));
        //部门
        $this->tpl->assign("classid", $this->m->getClassId());
        $this->tpl->display("user/edit.tpl");
    }

    public function saveedit() {

        $id = $this->m->saveedit();
        header("location:/user/edit?uid={$id}");
    }

    public function competence() {
        $uid = $this->input->get('uid');
        $detail = $this->m->getuserdetail($uid);
		$res = $this->fun->getdatabasesdetail("d_group"," AND id='".$detail["department"]."'");
        $power = explode(",", $detail['power']);
		if($res["rules"])
		{
			 $rules = explode(",", $res['rules']);
			 $this->tpl->assign("rules", $rules);
		}else{
			 $this->tpl->assign("rules", array());
		}
        $this->tpl->assign("power", $power);
        $this->tpl->assign("detail", $detail);
        $menudb = $this->m->getmenudblink();
        $this->tpl->assign("menudblist", $menudb);


        $this->tpl->display("user/competence.tpl");
    }

    public function savecompetence() {
        $competenceID = $this->input->post('competenceID');
        $uid = $this->input->post('uid');
        $this->m->savecompetence($competenceID, $uid);
        header("location:/user/competence?uid={$uid}");
    }

    public function department() {

        $list = $this->fun->getdatabaseslist('db_classid');
        $this->tpl->assign("list", $list);
        $this->tpl->display("user/department.tpl");
    }

    public function adddepartment() {

        $list = $this->fun->getdatabaseslist('d_group');
        $this->tpl->assign("list", $list);
        $this->tpl->display("user/adddepartment.tpl");
    }

    public function editdepartment() {

        $id = $this->input->get('id');
        $detail = $this->m->getdepartmentdetail($id);
        $this->tpl->assign("detail", $detail);
        $this->tpl->display("user/editdepartment.tpl");
    }

    public function saveadddepartment() {

        $this->m->saveadddepartment();
        header("location: /user/department");
    }

    public function saveeditdepartment() {

        $this->m->saveeditdepartment();
        header("location: /user/department");
    }

    public function deldepartment() {

        $this->m->deldepartment();
        header("location: /user/department");
    }

    public function usergroup() {

        $list = $this->fun->getdatabaseslist('d_group');
        $this->tpl->assign("list", $list);
        $this->tpl->display("user/usergroup.tpl");
    }

    public function addusergroup() {

        $id = $this->input->get('id');
        $detail = $this->m->getuserdetail($id);
        $power = explode(",", $detail['power']);
        $this->tpl->assign("power", $power);
        $this->tpl->assign("detail", $detail);
        $menudb = $this->m->getmenudblink();
        $this->tpl->assign("menudblist", $menudb);

        $list = $this->fun->getdatabaseslist('d_group');
        $this->tpl->assign("list", $list);
        $this->tpl->display("user/addusergroup.tpl");
    }

    public function saveaddusergroup() {

        $result = $this->m->saveaddusergroup();

        header("location: /user/usergroup");
    }

    public function editusergroup() {

        $id = $this->input->get('id');
        $detail = $this->m->getusergroupdetail($id);
        $power = explode(",", $detail['rules']);
        $this->tpl->assign("power", $power);
        $this->tpl->assign("detail", $detail);
        $menudb = $this->m->getmenudblink();
        $this->tpl->assign("menudblist", $menudb);

        $this->tpl->assign("usergroup", $this->fun->getdatabaseslist('d_menu', "", '*', ' order by id desc'));
        $this->tpl->display("user/editusergroup.tpl");
    }

    public function saveeditusergroup() {

        $this->m->saveeditusergroup();

        header("location: /user/usergroup");
    }

    public function delusergroup() {

        $this->m->delusergroup();
        header("location: /user/usergroup");
    }

    public function adduser() {

        if (empty($_POST)) {

            //查所有店铺
            $this->tpl->assign("d_shop", $this->fun->getdatabaseslist('d_shop', '', 'id,name', ' order by name asc'));
            //查所有仓库
            $this->tpl->assign("overseawarehouse", $this->fun->getdatabaseslist('t_overseawarehouse', '', 'id,name', ' order by id desc'));
            //用户组
            $this->tpl->assign("usergroup", $this->fun->getdatabaseslist('d_group', "", '*', ' order by id desc'));
            //部门
            $this->tpl->assign("classid", $this->fun->getdatabaseslist('db_classid'));

            $menudb = $this->m->getmenudblink();
            $this->tpl->assign("menudblist", $menudb);

            $list = $this->fun->getdatabaseslist('d_group');
            $this->tpl->assign("list", $list);
            $this->tpl->display("user/adduser.tpl");
        } else {

            $this->m->saveadduser();
            header("location: /management/userlist");
        }
    }

    public function edituser() {

        if (empty($_POST)) {

            $this->tpl->display("user/edituser.tpl");
        } else {

            $this->m->saveedituser();
			 header("location: /login/logout");
        }
    }

	public function Bindingshops()
	{
		 if (empty($_POST)) {
			  $nowpage = $_REQUEST["pageid"];
				if ($nowpage == "")
					$nowpage = 1;
				$wheres = "";
				$where = " AND del_flag =0";
				$other = trim($this->input->get_post('other'));
				if ($other) {
					$wheres .= " AND (name like '" . $other . "' )";
					$this->tpl->assign("other", $other);
				}else{

				$wheres .= " AND uploadID = '".$_COOKIE["userid"]."'";
				}
				$platformID = $this->input->get_post('platformID');
				if ($platformID) {
					$wheres .= " AND platfrom = '".$platformID."'";
					$this->tpl->assign("platformID", $platformID);
				}
				if($wheres){
					$list = $this->fun->getalllist('`d_shop`', $where.$wheres, $nowpage, $if_csv, $this->pageSize);
				
					$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
					$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
					$this->tpl->assign("total", $list["total"]);
					$this->tpl->assign("pageList", $pageList);
					$this->tpl->assign("list", $list["list"]);
				}
				$this->tpl->assign("platform", $this->fun->getdatabaseslist('db_platform'));
                $nowuser=$_COOKIE["username"];
                $this->tpl->assign('nowuser',$nowuser);
				$this->tpl->display("user/bindingshops.tpl");
        }
		else{
			$other = $this->input->get_post('other');
			$platformID = $this->input->get_post('platformID');
			$type =  $this->input->get_post('type');
			if($type==1)
			{
				$updateoper = $this->input->get_post('updateoper');
				$updateoper = $this->fun->ReplaceSpecialChar($updateoper,2);
				if($updateoper){
					$username = explode(",",$updateoper);
					$username =array_unique(array_filter($username));
					
					foreach($username as $user)
					{
						$usernames = $this->fun->userinfo(trim($user));
						if(!$usernames)
						{
							die("{$user} 这个用户不存在！请先添加用户！");
						}
					}
				
				}

				$newuser = implode(",",$username);
				$post["otherUpload"] = $newuser;
				$this->db->update('d_shop',$post,"id=".$this->input->post('shopid'));
				 header("location: $_SERVER[HTTP_REFERER]");
			}else{
				$user = $this->fun->userinfo(trim($this->input->post('updateoper')));
				if($user)
				{
					$post["updateoper"] = $user["name"];
					$post["uploadID"] = $user["id"];
					$this->db->update('d_shop',$post,"id=".$this->input->post('shopid'));
					 header("location: $_SERVER[HTTP_REFERER]");
				}
			}
			
		}
	}

    public function deluser() {

        $this->m->deluser();
        header("location: /management/userlist");
    }


	public function ajaxbutton()
	{
		$op = $this->input->get_post('op');
		if($op =="getuser")
		{
			$res = $this->fun->getdatabasesdetail('d_user'," AND username='".trim($this->input->post('username'))."'");
			if($res)
			{
				echo 201;
				die;
			}
		}
		if($op =="getusername")
		{
			$res = $this->fun->getdatabasesdetail('d_user'," AND name='".trim($this->input->post('updateoper'))."'");
			if($res)
			{
				echo 201;
				die;
			}
		}




	}
}
